package easeui.widget.chatrow;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.bean.YNHistory;

public class EaseChatRowHistoryVoice2 extends EaseChatRowHistoryFile2 {

    private ImageView voiceImageView;
    private TextView voiceLengthView;
    private ImageView readStatusView;
    private int direction;

    public EaseChatRowHistoryVoice2(Context context, YNHistory message, int position, BaseAdapter adapter) {
        super(context, message, position, adapter);
    }

    @Override
    protected void onInflateView() {
        direction = message.getFrom().equals(String.valueOf(application.getCurID())) ? 0 : 1;
        inflater.inflate(direction == 1 ?
                R.layout.ease_row_received_voice : R.layout.ease_row_sent_voice2, this);
    }

    @Override
    protected void onFindViewById() {
        voiceImageView = ((ImageView) findViewById(R.id.iv_voice));
        voiceLengthView = (TextView) findViewById(R.id.tv_length);
        readStatusView = (ImageView) findViewById(R.id.iv_unread_voice);
    }

    @Override
    protected void onSetUpView() {
        int len = message.getPayload().getBodies().get(0).getLength();
        if (len > 0) {
            voiceLengthView.setText(len + "\"");
            voiceLengthView.setVisibility(View.VISIBLE);
        } else {
            voiceLengthView.setVisibility(View.INVISIBLE);
        }
        if (EaseChatRowVoicePlayClickListener2.playMsgId != null
                && EaseChatRowVoicePlayClickListener2.playMsgId.equals(message.getMsg_id()) && EaseChatRowVoicePlayClickListener2.isPlaying) {
            AnimationDrawable voiceAnimation;
            if (direction == 1) {
                voiceImageView.setImageResource(R.anim.voice_from_icon);
            } else {
                voiceImageView.setImageResource(R.anim.voice_to_icon);
            }
            voiceAnimation = (AnimationDrawable) voiceImageView.getDrawable();
            voiceAnimation.start();
        } else {
            if (direction == 1) {
                voiceImageView.setImageResource(R.drawable.ease_chatfrom_voice_playing);
            } else {
                voiceImageView.setImageResource(R.drawable.ease_chatto_voice_playing);
            }
        }
        if (direction == 1) {
            readStatusView.setVisibility(View.INVISIBLE);
            progressBar.setVisibility(View.INVISIBLE);
            return;
        }
    }

    @Override
    protected void onUpdateView() {
        super.onUpdateView();
    }

    @Override
    protected void onBubbleClick() {
        new EaseChatRowVoicePlayClickListener2(message, voiceImageView, readStatusView, adapter, activity,direction).onClick(bubbleLayout);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (EaseChatRowVoicePlayClickListener2.currentPlayListener != null && EaseChatRowVoicePlayClickListener.isPlaying) {
            EaseChatRowVoicePlayClickListener2.currentPlayListener.stopPlayVoice();
        }
    }

}
